package com.xuedongzhilian.controller;

import com.xuedongzhilian.dto.Result;
import com.xuedongzhilian.dto.RunningRecordDTO;
import com.xuedongzhilian.entity.RunningRecord;
import com.xuedongzhilian.service.IRunningRecordService;
import com.xuedongzhilian.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 跑步记录控制器
 * </p>
 *
 * @author 学动智联团队
 * @since 2025-10-14
 */
@Slf4j
@RestController
@RequestMapping("/running")
public class RunningRecordController {

    @Resource
    private IRunningRecordService runningRecordService;

    /**
     * 开始跑步
     */
    @PostMapping("/start")
    public Result startRunning(@RequestBody RunningRecordDTO runningRecordDTO) {
        RunningRecord runningRecord = new RunningRecord();
        BeanUtils.copyProperties(runningRecordDTO, runningRecord);
        runningRecord.setUserId(UserHolder.getUser().getId());
        runningRecord.setStartTime(LocalDateTime.now());
        runningRecord.setCreateTime(LocalDateTime.now());
        runningRecord.setUpdateTime(LocalDateTime.now());
        
        boolean success = runningRecordService.save(runningRecord);
        if (success) {
            return Result.ok(runningRecord.getId());
        } else {
            return Result.fail("开始跑步失败");
        }
    }

    /**
     * 结束跑步
     */
    @PostMapping("/end")
    public Result endRunning(@RequestBody RunningRecordDTO runningRecordDTO) {
        RunningRecord runningRecord = new RunningRecord();
        BeanUtils.copyProperties(runningRecordDTO, runningRecord);
        runningRecord.setEndTime(LocalDateTime.now());
        runningRecord.setUpdateTime(LocalDateTime.now());
        
        boolean success = runningRecordService.updateById(runningRecord);
        if (success) {
            return Result.ok(runningRecord);
        } else {
            return Result.fail("结束跑步失败");
        }
    }

    /**
     * 获取用户跑步记录
     */
    @GetMapping("/records")
    public Result getUserRunningRecords() {
        Long userId = UserHolder.getUser().getId();
        List<RunningRecord> records = runningRecordService.lambdaQuery()
                .eq(RunningRecord::getUserId, userId)
                .orderByDesc(RunningRecord::getCreateTime)
                .list();
        return Result.ok(records);
    }

    /**
     * 获取跑步记录详情
     */
    @GetMapping("/{id}")
    public Result getRunningRecord(@PathVariable Long id) {
        RunningRecord record = runningRecordService.getById(id);
        if (record == null) {
            return Result.fail("跑步记录不存在");
        }
        return Result.ok(record);
    }

    /**
     * 删除跑步记录
     */
    @DeleteMapping("/{id}")
    public Result deleteRunningRecord(@PathVariable Long id) {
        boolean success = runningRecordService.removeById(id);
        if (success) {
            return Result.ok("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }
}